package cn.swing.his.element.webapi.controller;

import cn.swing.his.common.web.JsonResult;
import cn.swing.his.element.service.IArticleService;
import cn.swing.his.pojo.element.entity.Article;
import cn.swing.his.pojo.element.entity.Category;
import cn.swing.his.pojo.element.vo.ArticleDetailsVO;
import cn.swing.his.pojo.element.vo.ArticleHomeVO;
import cn.swing.his.pojo.element.vo.CategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value="/articles",produces = "application/json;charset=utf-8")
@Api(tags = "前台元素")
public class ArticleController {

    @Autowired(required = false)
    private IArticleService articleService;

    //添加类别
    @PostMapping("/insertCategory")
    @ApiOperation("插入类别")
    public JsonResult<Void> categoryInsertResult(Category category) {
        articleService.insertCategory(category);
        return JsonResult.ok();
    }
    //删除类别
    @PostMapping("/deleteCategory")
    @ApiOperation("删除类别")
    public JsonResult<Void> categoryDeleteResult(Long id) {
        articleService.deleteCategoryById(id);
        return JsonResult.ok();
    }
    @GetMapping("/selectCategory")
    @ApiOperation("分类显示")
    public JsonResult<List<CategoryVO>> selectCategory(){
        List<CategoryVO > CategoryVOS = articleService.selectCategory();
        return JsonResult.ok("",CategoryVOS);
    }
    //根据文章id查询文章
    @GetMapping("/articlesCategory")
    @ApiOperation("分类查询文章")
    public JsonResult<List<ArticleDetailsVO>> articleCategoryListResult(Integer categoryId) {
        List<ArticleDetailsVO> articleDetailsVOS = articleService.selectArticleByCategoryId(categoryId);
        return JsonResult.ok(articleDetailsVOS);
    }
    //上传文章
    @PostMapping("/uploadArticle")
    @ApiOperation("上传文章")
    public JsonResult<Void> uploadArticleResult(String content){
        Article article = new Article();
        article.setContent(content);
        System.out.println(article.getContent());
        articleService.uploadArticle(article);
        return JsonResult.ok();
    }
    //操作：删除文章
    @PostMapping("/delete")
    @ApiOperation("删除文章")
    public JsonResult<Void> articleDeleteResult(Long id) {
        articleService.deleteArticleById(id);
        return JsonResult.ok();
    }

    @GetMapping("/articlesDetailsHome")
    @ApiOperation("文章显示")
    public JsonResult<List<ArticleHomeVO>> selectArticleDetailsHome(){
        System.out.println("文章显示");
        List<ArticleHomeVO> articleHomeVOS = articleService.selectArticleHome();
        return JsonResult.ok("fanhoumengSB",articleHomeVOS);
    }
}
